package com.bit.alarm.controller;

import com.bit.alarm.entity.AlarmMessage;
import com.bit.alarm.mail.Mail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/alarm")
public class AlarmController {

    @Autowired
    private Mail mail;

    @RequestMapping("/handler")
    public String handler(@RequestBody List<AlarmMessage> alarmMessages){
        log.info("接收到告警: {}", alarmMessages);
        mail.sendMail("to_lcf@126.com", buildContent(alarmMessages));
        return "接收到告警";
    }

    private String buildContent(List<AlarmMessage> alarmMessages) {
        StringBuilder builder = new StringBuilder();
        builder.append("告警详情:");
        for (AlarmMessage message: alarmMessages){
            builder.append("<br/>scopeId:").append(message.getScopeId())
                    .append("<br/>scope:").append(message.getScope())
                    .append("<br/>name:").append(message.getName())
                    .append("<br/>id0:").append(message.getId0())
                    .append("<br/>ruleName:").append(message.getRuleName())
                    .append("<br/>alarmMessage:").append(message.getAlarmMessage())
                    .append("<br/>startTime:").append(message.getStartTime())
                    .append("<br/>-----------");
        }
        return builder.toString();
    }
}
